package com.dykj.fireFighting.web.buildmanger;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryService;
import com.dykj.fireFighting.api.base.video.entity.PartitionsVideo;
import com.dykj.fireFighting.api.base.video.service.IPartitionsVideoService;
import com.dykj.fireFighting.api.socialUnit.buildmanger.dto.DevicePointsDto;
import com.dykj.fireFighting.api.socialUnit.buildmanger.dto.PartitionsDto;
import com.dykj.fireFighting.api.socialUnit.buildmanger.entity.Partitions;
import com.dykj.fireFighting.api.socialUnit.buildmanger.service.IPartitionsService;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.PartitionsDevice;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IPartitionsDeviceService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;



/**
 * 建筑管理-分区接口
 *
 * @Author aixiaolan
 * @Date  2021-06-23 14:59:35
 */
@Slf4j
@Api(value = "建筑管理-分区接口", tags = {"建筑管理-分区接口"})
@RestController
@RequestMapping("buildmanger/partitions")
public class PartitionsController {

    @DubboReference
    private IPartitionsService partitionsService;

    @DubboReference
    private IPartitionsDeviceService partitionsDeviceService;


    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }


    /**
     * 建筑管理-分区列表
     *
     * @return
     */
    @AutoLog(value = "建筑管理-分区-分页列表查询")
    @ApiOperation(value = "建筑管理-分区分页列表", notes = "建筑管理-分区分页列表")
    @GetMapping("/list")
//    @RequiresPermissions("buildmanger/partitions/list")
    public CommonResult<PageVo<Partitions>> treeGrid(PartitionsDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<Partitions> page = partitionsService.pageList(commonDto);
        return CommonResult.success(page);
    }


    /**
     * 建筑管理-分区列表
     *
     * @return
     */
    @AutoLog(value = "建筑管理-分区-分页列表查询")
    @ApiOperation(value = "建筑管理-分区分页列表", notes = "建筑管理-分区分页列表")
    @GetMapping("/list2")
    public CommonResult<List<Partitions>> getPartitionsList() {
        List<Partitions> page = partitionsService.getPartitionsList(getEnterpriseId());
        return CommonResult.success(page);
    }


    /**
     * 获取建筑管理-分区信息
     */
    @AutoLog(value = "建筑管理-分区-获取建筑管理-分区信息")
    @ApiOperation(value = "获取建筑管理-分区信息", notes = "获取建筑管理-分区信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
//    @RequiresPermissions("buildmanger/partitions/info")
    public CommonResult<Partitions> info(@PathVariable("id") String id){
        Partitions partitions = partitionsService.getById(id);
        return CommonResult.success(partitions);
    }

    /**
     * 保存建筑管理-分区信息
     */
    @AutoLog(value = "建筑管理-分区-保存建筑管理-分区信息")
    @ApiOperation(value = "保存建筑管理-分区信息", notes = "保存建筑管理-分区信息")
    @PostMapping("/save")
//    @RequiresPermissions("buildmanger/partitions/save")
    public CommonResult save(@Valid @RequestBody Partitions partitions) {
        try {
            partitions.setCreateTime(new Date());
            partitions.setUpdateTime(new Date());
            partitions.setCreateBy(getName());
            partitions.setUnitId(getEnterpriseId());
                partitionsService.save(partitions);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改建筑管理-分区信息
     */
    @AutoLog(value = "建筑管理-分区-修改建筑管理-分区信息")
    @ApiOperation(value = "修改建筑管理-分区信息", notes = "修改建筑管理-分区信息")
    @PostMapping("/update")
//    @RequiresPermissions("buildmanger/partitions/update")
    public CommonResult update(@Valid @RequestBody Partitions partitions) {
        try {
            partitions.setUpdateTime(new Date());
            partitions.setUpdateBy(getName());
                partitionsService.updateById(partitions);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除建筑管理-分区信息
     */
    @AutoLog(value = "建筑管理-分区-删除建筑管理-分区信息")
    @ApiOperation(value = "删除建筑管理-分区信息", notes = "删除建筑管理-分区信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
//    @RequiresPermissions("buildmanger/partitions/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                partitionsService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    @PostMapping("/saveDevice")
    @AutoLog(value = "建筑管理-分区-保存/修改设备点位信息")
    public CommonResult saveDevice(@Valid @RequestBody DevicePointsDto devicePointsDto){
        try{
            System.out.println(devicePointsDto);
            partitionsDeviceService.insertDevicePoint(devicePointsDto.getPartitionId(), devicePointsDto.getDevivePoints());
            return CommonResult.success("保存成功");
        }catch (Exception e){
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    @GetMapping("/getDevice")
    @AutoLog(value = "建筑管理-分区-获取设备点位信息")
    public CommonResult getDevice(@RequestParam("partitionId") String partitionId){
        List<PartitionsDevice> devices = partitionsDeviceService.getDevices(partitionId);
        return  CommonResult.success(devices);
    }

    @GetMapping("getPartitionsDeviceAndVideo")
    @AutoLog(value = "大数据页面查询设备")
    public CommonResult getPartitionsDevice(@RequestParam("partitionId") String partitionId){
        List<PartitionsDevice> list=partitionsDeviceService.getList(partitionId);
        return CommonResult.success(list);
    }
}
